package com.logistics.distribution;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.logistics.database.Distributiondatabase;

public class Categorylv1servlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	Distributiondatabase ddb;
	public Categorylv1servlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	       String type = request.getParameter("type");
	       System.out.println("gettype="+type);
	       if(type.equals("delete"))
	       {
	    	   System.out.println("type=delete");
	    	   String name=new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
	    	   System.out.println("name="+name);
	    	   ddb.categoryLevel1Delete(name);
	       }
	       else if(type.equals("insert"))
	       {
	    	   System.out.println("type=insert");
	    	   String name=new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
	    	   System.out.println("name="+name);
	    	   ddb.categoryLevel1Insert(name);
	       }
	       ResultSet rs=ddb.categoryLevel1Query();
	       ArrayList<String> category = new ArrayList<String>();
	       try {
	    	   while(rs.next())
			   {
				   category.add(rs.getString(1));
			   }
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       request.setAttribute("categorylv1", category);
	       request.getRequestDispatcher("../distribution/Categorylv1.jsp").forward(request, response); 
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		ddb=new Distributiondatabase();
	}

}
